package com.ruoyi.web.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.domain.dto.query.RecordMenuQueryDTO;
import com.ruoyi.mapper.RecordMenuMapper;
import org.springframework.stereotype.Service;
import com.ruoyi.domain.entity.RecordMenuVisit;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RecordMenuService {

    @Resource
    private RecordMenuMapper recordMenuMapper;

    public void recordMenuVisit(RecordMenuQueryDTO menu) {
        RecordMenuVisit visit = new RecordMenuVisit();
        visit.setPath(menu.getPath());
        if (menu.getQuery()!=null) {
            visit.setQueryStr(JSON.toJSONString(menu.getQuery()));
        }

        visit.setTitle(menu.getTitle());
        recordMenuMapper.insert(visit);
    }

    public List<RecordMenuVisit> getMenuRecord(Long userId) {
        QueryWrapper<RecordMenuVisit> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("create_user_id", userId);
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 10");
        return recordMenuMapper.selectList(queryWrapper);
    }

}
